package tree.迭代bfs;

import po.TreeNode;

import java.util.Deque;
import java.util.LinkedList;

/**
 * @author songZiHao
 * @version 1.0.0
 * @ClassName 找树左下角的值513.java
 * @Description https://leetcode-cn.com/problems/find-bottom-left-tree-value/
 * @createTime 2021年06月01日 19:31:00
 */
public class 找树左下角的值513 {

    public int findBottomLeftValue(TreeNode root) {
        Deque<TreeNode> deque = new LinkedList<>();
        deque.offer(root);
        int result = 0;
        while (!deque.isEmpty()) {
            for (int i = 0; i < deque.size(); i++) {
                TreeNode node = deque.poll();
                result = node.val;
                //1. 如果左节点都是空，就找右节点
                if (node.right != null) {
                    deque.offer(node.right);
                }
                if (node.left != null) {
                    deque.offer(node.left);
                }
            }
        }
        return result;
    }
}
